#include "camerawidget.h"
#include "xqviewfinderwidget.h"
#include "xqcamera.h"
#include "myqevent.h"
#include "commondef.h"
#include "toolkit.h"
#include "mainwindow.h"
#include "QProgressIndicator.h"
#include <QPainter>
#include <QMouseEvent>

CameraWidget::CameraWidget(QWidget *parent) :
    QWidget(parent),
    m_pi(NULL),
    m_bbig(false),
    m_bShowCameraBtn(false),
    m_bShowBackBtn(false)
{
    m_viewFinderWidget = new XQViewFinderWidget(this);
    m_viewFinderWidget->setGeometry(0,0,245,170);

    connect(&m_effectThread,SIGNAL(effectThreadOver()),this,SLOT(effectThreadOver()));
}

CameraWidget::~CameraWidget()
{   
    if(m_camera != NULL)
    {
        m_viewFinderWidget->stop();
        m_camera->close();
        delete m_camera;
        qWarning("CameraWidget destruction");
    }
}

void CameraWidget::customEvent ( QEvent * event )
{
    int type = event->type();
    if ( type == MY_EVENT_TPPE )
    {
        m_camera = new XQCamera();

        m_viewFinderWidget->setCamera(*m_camera);
        m_viewFinderWidget->setViewfinderSize(QSize(480, 360));
        m_viewFinderWidget->showMaximized();

        connect(m_camera, SIGNAL(captureCompleted(QImage*)), this, SLOT(captured(QImage*)));
        connect(m_camera, SIGNAL(cameraReady()), m_viewFinderWidget, SLOT(start()));
        connect(this, SIGNAL(cameraclicked()), m_camera, SLOT(capture()));

        m_camera->open(0);
        m_camera->open();
    }
}

void CameraWidget::captured(QImage* snapImage)
{
    if(m_pi == NULL)
    {
        m_pi = new QProgressIndicator(this);
        m_pi->setWindowModality(Qt::WindowModal);
        m_pi->raise();
        if(m_bbig)
        {
           m_pi->setGeometry(270,130,100,100);
        }
        else
            m_pi->setGeometry(62,36,100,100);
        m_pi->startAnimation();
        m_pi->show();
    }

    ToolKit::m_mainWindow->m_bpressable = false;
    m_viewFinderWidget->stop();
    m_camera->releaseImageBuffer();
    ToolKit::m_cameraImage = snapImage;
    m_effectThread.start();
}

void CameraWidget::capture()
{
    emit cameraclicked();
}

void CameraWidget::effectThreadOver()
{
    if(m_pi != NULL)
    {
        m_pi->stopAnimation();
        delete m_pi;
        m_pi = NULL;
    }

    ToolKit::m_mainWindow->m_bpressable = true;

    emit showPreview();
}

void CameraWidget::paintEvent(QPaintEvent *e)
{
    QPainter p(this);
    if(m_bbig)
    {
        p.drawImage(480,0,QImage(":/image/bigcamera/bg.jpg"));
        if(m_bShowCameraBtn)
        {
            p.drawImage(480,0,QImage(":/image/bigcamera/480,0.png"));
        }
        else if(m_bShowBackBtn)
        {
            p.drawImage(480,275,QImage(":/image/bigcamera/480,275.png"));
        }
    }
}

void CameraWidget::mousePressEvent(QMouseEvent *e)
{
    if(m_bbig)
    {
        if(QRectF(480.0,0.0,160.0,85.0).contains(e->pos()))
        {
            m_bShowCameraBtn = true;
        }
        else if(QRectF(480,275,160,85).contains(e->pos()))
        {
            m_bShowBackBtn = true;
        }
        update();
    }
    else
    {
       setGeometry(0,0,640,360);
       m_viewFinderWidget->setGeometry(0,0,480,360);
       m_bbig = true;
    }
}

void CameraWidget::mouseReleaseEvent(QMouseEvent *e)
{
    if(m_bbig)
    {
        if(m_bShowCameraBtn)
        {
            m_bShowCameraBtn = false;
            if(QRectF(480.0,0.0,160.0,85.0).contains(e->pos()))
            {
                capture();
            }
        }
        else if(m_bShowBackBtn)
        {
            m_bShowBackBtn = false;
            if(QRectF(480,275,160,85).contains(e->pos()))
            {
                m_bbig = false;
                setGeometry(183,135,245,170);
                m_viewFinderWidget->setGeometry(0,0,245,170);
            }
        }
        update();
    }
}
